package cn.xo68.boot.auth.server.service;

import java.util.List;

import cn.xo68.boot.auth.core.common.AuthConstants;
import cn.xo68.boot.auth.server.domain.OauthClient;
import cn.xo68.boot.auth.server.mapper.OauthClientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;



/** 
 * Oauth2Client Service
 * @author wuyy(16349023@qq.com)
 * @date 2018年08月04日 13:01:10
 *
 */
@CacheConfig(cacheNames = {AuthConstants.CACHE_NAME_OAUTH2_CLIENT})
@Service
public class OauthClientService{

    @Autowired
    private OauthClientMapper oauthClientMapper;

    @Cacheable(key = "#clientId")
    public OauthClient get(String clientId) {
        return  oauthClientMapper.get(clientId);
    }
    @CachePut(key = "#oauthClient.clientId")
    public OauthClient insert(OauthClient oauthClient) {
        oauthClientMapper.insert(oauthClient);
        return oauthClient;
    }
    @CacheEvict(key = "#oauthClient.clientId")
    public void update(OauthClient oauthClient) {
        oauthClientMapper.update(oauthClient);
    }
    @CacheEvict(key = "#clientId")
    public void delete(String clientId) {
        oauthClientMapper.delete(clientId);
    }

    public List<OauthClient> list() {
        return oauthClientMapper.list();
    }



    public PageInfo<OauthClient> list(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize,true);
        List<OauthClient> docs = oauthClientMapper.list();
        PageInfo<OauthClient> pageInfo = new PageInfo<>(docs);
        return pageInfo;
    }
}